package com.petmarket.controller;


import com.alibaba.fastjson.JSON;
import com.petmarket.client.LoginClient;
import com.petmarket.client.TransferClient;
import com.petmarket.client.UserClient;
import com.petmarket.common.PageResult;
import com.petmarket.common.Result;
import com.petmarket.entity.LoginDetail;
import com.petmarket.entity.Pet;
import com.petmarket.entity.Transfer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigInteger;

@RestController
@RequestMapping("/Transfer")
@Slf4j
public class TransferController {


    @Autowired
    private TransferClient transferClient;
    @Autowired
    private UserClient userClient;
    @Autowired
    private LoginClient loginClient;

    @PostMapping("/getTransferPage")
    public Result<PageResult<Transfer>> getTransferPage(@RequestBody Transfer transfer){
        try{
            if (log.isInfoEnabled()){
                log.info("UserController.getUserPage:{}", JSON.toJSONString(transfer));
            }
            PageResult<Transfer> result = transferClient.getPetPage(transfer.getPageNo(),transfer.getPageSize(),transfer.getBuyerId());
            return Result.ok(result);
        }
        catch (Exception e){
            return Result.fail("获取列表失败");
        }
    }

    @PostMapping("/remove")
    public Result<Boolean> remove(@RequestBody Transfer transfer){
        try{
            if(log.isInfoEnabled()){
                log.info("TransferController.getUserPage:{}", JSON.toJSONString(transfer));
            }
            transferClient.remove(transfer);

            LoginDetail loginDetail=new LoginDetail();
            BigInteger sellerId=new BigInteger(transfer.getSellerId());
            BigInteger buyerId=new BigInteger(transfer.getBuyerId());
            BigInteger amount=new BigInteger(transfer.getAmount());
            loginDetail.setId(sellerId);
            LoginDetail detail = loginClient.query(loginDetail);
            userClient.transfer(sellerId,buyerId,amount,detail.getPrivateKey());
            return Result.ok("删除成功");
        }
        catch(Exception e){
            return Result.fail("删除失败");
        }
    }





}
